package com.ss.ttm.player;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.support.v4.util.LongSparseArray;
import android.util.Log;

import inline.PackageUtils;

class o implements Handler.Callback {
    private static o a;
    private Context b;
    private n c;
    private Handler d;
    private Object e;
    private LongSparseArray f;

    o() {
        this.e = new Object();
        this.f = new LongSparseArray();
    }

    protected final void a(long arg2, String arg4) {
        p v0 = this.b(arg2);
        if (v0 != null) {
            v0.b(arg4);
        }
    }

    static int a(p arg3) {
        arg3.a(20000, 0, 0, null);
        return 0;
    }

    protected static Context a() {
        return o.a.b;
    }

    protected static p a(Context context) {
        p v0_7 = null;
        n v0_4;
        int v0_3;
        StringBuilder v6;
        o v5;
        Object v4;
        long v12 = 0;
        p v1 = null;
        Class v3 = o.class;
        synchronized (v3) {
            try {
                if (o.a == null) {
                    o v0_1 = new o();
                    o.a = v0_1;
                    v0_1.b = context.getApplicationContext();
                    o.a.d = new Handler(o.a);
                }

                v4 = o.a.e;
                synchronized (v4) {
                    v5 = o.a;
                    if (v5.c != null) {
                    } else {
                        v5.c = new n(v5);
                        v6 = new StringBuilder();
                        v6.append("will start service to start.time:" + android.support.design.a.w()).append("\r\n");
                        Intent v7 = new Intent(v5.b, s.class);
                        v7.putExtra("real_package_name", "com.ss.ttm");
                        if (!android.support.design.a.a((ContextWrapper) v5.b, v7, v5.c)) {
                            v6.append("bindService fail.time:" + android.support.design.a.w()).append("\r\n");
                            m.b(16, v6.toString());
                            v0_3 = 0;
                        } else if (v5.c()) {
                            m.b(10, m.a(10, 0) + 1);
                            v6.append("start service timeout.time:" + android.support.design.a.w()).append("\r\n");
                            m.b(16, v6.toString());
                            v0_3 = 0;
                        } else if (!v5.c.a()) {
                            v0_3 = 10;
                            m.b(v0_3, 0);
                            v6.append("start service is ok.time:" + System.currentTimeMillis()).append("\r\n");
                            m.b(16, v6.toString());
                            v0_3 = 1;
                        } else {
                            v0_4 = v5.c;
                            if (v0_4.a == null) {
                                v0_3 = 10;
                                m.b(v0_3, 0);
                                v6.append("start service is ok.time:" + System.currentTimeMillis()).append("\r\n");
                                m.b(16, v6.toString());
                                v0_3 = 1;
                            } else {
                                int v2 = 19;
                                m.b(v2, v0_4.a.getStringOption(0, 27));
                                m.b(20, v0_4.a.getStringOption(0, 28));
                                v0_4.a.a(v0_4.b);
                                v0_3 = 10;
                                m.b(v0_3, 0);
                                v6.append("start service is ok.time:" + System.currentTimeMillis()).append("\r\n");
                                m.b(16, v6.toString());
                                v0_3 = 1;
                            }
                        }
                        if (v0_3 == 0) {
                            m.b(16, m.a(16, "not find") + "\r\nstart server is error.");
                            v0_7 = v1;
                            return v0_7;
                        }
                    }
                    if (v5.c != null && (v5.c.a())) {
                        v0_7 = new p(v5.c);
                        if (v0_7.a == v12) {
                            m.b(16, "create player handle is zore");
                            v0_7 = v1;
                        } else {
                            if (v5.f.size() > 3) {
                                Log.e("ttplayer", "!!!!!!!!!!!!!!!!!!create to more player object.you must release.!!!!!!!!!!!!!!!");
                            }
                            v5.f.put(v0_7.a, v0_7);
                            v5.d.removeMessages(10000);
                        }
                    } else {
                        m.b(16, "connection is null or not connectioned.");
                        v0_7 = v1;
                    }
                    return v0_7;
                }
            } catch (RemoteException v0_5) {
                v0_5.printStackTrace();
            }
            return v0_7;
        }
    }

    public static void a(long arg6) {
        Class v1 = o.class;
        synchronized (v1) {
            if (o.a != null) {
                o v0_1 = o.a;
                v0_1.f.remove(arg6);
                if (v0_1.f.size() <= 0) {
                    Message v2 = v0_1.d.obtainMessage();
                    v2.what = 10000;
                    v0_1.d.removeMessages(10000);
                    v0_1.d.sendMessageDelayed(v2, 600000);
                }
            }
        }
    }

    final p b(long arg6) {
        Object v0_2;
        p v0;
        p v1 = null;
        if (this.f.size() == 0) {
            v0 = v1;
            return null;
        }

        if (arg6 <= 0) {
            return (p) this.f.valueAt(0);
        }

        try {
            v0_2 = this.f.get(arg6);
            return (p) v0_2;
        } catch (Throwable v0_1) {
            v0_1.printStackTrace();
        }

        return ((p) null);
    }

    public final void b() {
        Throwable v1_1;
        String v0_1 = null;
        String v5 = null;
        String v1;
        int v3 = 0;
        int v2 = 1;
        int v4 = this.f.size();
        if (m.a(18, 1) > 1) {
            v1 = "not find breakpad crash file";
            try {
                v5 = android.support.design.a.C(this.b);
                if (v5 != null) {
                    v0_1 = "find breakpad crash file";
                } else {
                    v0_1 = v1;
                }
            } catch (Throwable v0) {

            }

            if (v4 <= 0 && v5 != null) {
                android.support.design.a.B(v5);
            }

            v1 = v0_1;
        } else {
            v1 = "port version is old";
        }

        int v5_1 = 21;
        int v0_2 = PackageUtils.i(this.b) ? 1 : 0;
        m.b(v5_1, v0_2);
        v0_2 = 22;
        if (!PackageUtils.j(this.b)) {
            v2 = 0;
        }

        m.b(v0_2, v2);
        m.b(23, PackageUtils.k(this.b));
        while (v3 < v4) {
            Object v0_3 = this.f.valueAt(v3);
            if (v0_3 != null) {
                ((p) v0_3).b("<" + o.class.getSimpleName()
                        + ",onServiceDisconnected," + android.support.design.a.w()
                        + ">player is will null.activity player:" + v4 + "," + v1 + "\r\n");
                o.a(((p) v0_3));
            }

            ++v3;
        }

        this.f.clear();
        this.c = null;
    }

    private boolean c() {
        boolean v1 = false;
        int v0 = 0;
        while (!this.c.a()) {
            if (v0 > 2000) {
                v1 = true;
            } else {
                long v2 = 10;
                try {
                    Thread.sleep(v2);
                    v0 += 10;
                } catch (Exception v2_1) {
                }

                continue;
            }

            return v1;
        }

        return v1;
    }

    public boolean handleMessage(Message arg5) {
        int v1 = arg5.what;
        if (10000 != v1) {
            return true;
        }

        Object v1_1 = this.e;
        synchronized (v1_1) {
            if (this.f.size() != 0) {
                return true;
            }

            m.b(12, "stop service.time:" + android.support.design.a.w());
            try {
                n.b();
                this.b.unbindService(this.c);
            } catch (Throwable v0) {
            }
            this.d.removeCallbacksAndMessages(null);
            this.b = null;
            o.a = null;
            this.d = null;
            this.c = null;
            return true;
        }
    }
}

